import 'package:auto_size_text_plus/auto_size_text_plus.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/widget/app_bar_x.dart';

import 'logic.dart';

class AdjustZeroRecordPage extends StatefulWidget {
  const AdjustZeroRecordPage({Key? key}) : super(key: key);

  @override
  State<AdjustZeroRecordPage> createState() => _AdjustZeroRecordPageState();
}

class _AdjustZeroRecordPageState extends State<AdjustZeroRecordPage> {
  final logic = Get.put(AdjustZeroRecordLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarX(
        title: Text("校准记录".tr),
        centerTitle: true,
        elevation: 0,
      ),
      backgroundColor: Get.theme.primaryColor,
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "ID:${logic.rightBleManager.deviceModel != null ? 'L:${logic.bleManager.deviceModel?.deviceID}-R:${logic.rightBleManager.deviceModel?.deviceID}' : logic.bleManager.deviceModel?.deviceID}".tr,
              style: TextStyle(fontSize: 12, color: Colors.white),
            ).marginOnly(top: 40, left: 28, bottom: 6),
            Expanded(
              child: Container(
                // margin: EdgeInsets.only(top: 28),
                width: double.infinity,
                padding: EdgeInsets.only(top: 28, left: 10, right: 10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(28),
                      topRight: Radius.circular(28)),
                ),
                child: Obx(() => ListView.builder(
                      itemBuilder: (context, index) {
                        final item = logic.items[index];
                        var title = "";
                        var result = "";
                        if (item.first != null && item.second != null){
                          title = "L:${translate(item.first?.first)} R:${translate(item.second?.first)}";
                          if (isSuccess(item.first?.first) && isSuccess(item.second?.first)){
                            result = "校准成功".tr;
                          }else{
                            result = "校准失败".tr;
                          }
                        }else{
                          title = translate(item.first?.first);
                          result = this.result(item.first?.first);
                        }
                        return Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text("${index + 1}").marginOnly(right: 16),
                            Expanded(
                              child: Column(
                                crossAxisAlignment:
                                CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  AutoSizeText(
                                    "${'校准结果'.tr}:".tr +
                                        title,
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Colors.black87,
                                        fontWeight: FontWeight.normal),
                                  ),
                                  Text(item.first?.second ?? item.second?.second ?? '').marginOnly(top: 4)
                                ],
                              ),
                            ),
                            Text(
                              result,
                              style: TextStyle(
                                  color: Get.theme.primaryColor),
                            ),
                          ],
                        ).paddingSymmetric(horizontal: 16, vertical: 6);
                      },
                      itemCount: logic.items.length,
                    )),
              ),
            ),
          ],
        ),
      ),
    );
  }

  String translate(int? code) {
    switch (code) {
      case 0xffff:
        return "--";
      case 0xfffe:
        return "充电中校准失败".tr;
      case 0xfffd:
        return "没有垂直校准失败".tr;
      case null:
        return "";
      default:
        return "$code";
    }
  }

  String result(int? code) {
    switch (code) {
      case 0xffff:
      case 0xfffe:
      case 0xfffd:
        return "校准失败".tr;
      case null:
        return "";
      default:
        return "校准成功".tr;
    }
  }

  bool isSuccess(int? code) {
    switch (code) {
      case 0xffff:
        return false;
      case 0xfffe:
        return false;
      case 0xfffd:
        return false;
      case null:
        return false;
      default:
        return true;
    }
  }

  @override
  void dispose() {
    Get.delete<AdjustZeroRecordLogic>();
    super.dispose();
  }
}
